
var util = require('../../utils/util.js')
const config = require('../../utils/config.js');
var my = require("../../utils/request.js");

Page({
  data: {

    //接口地址
    ActivityByID: "activity/",
    ActivityJoin: "activity/",
    sendMsg: "activity/sendMsg",
    //活动id
    id: '',
    formId: '',
    price:''
  },
  onLoad: function (options) {
    var that = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 500
    })
    var that = this
    this.setData({
      id: options.id,
      price: options.price
    }) 
  },

  //报名
  join:function(e){

    var that = this;
    that.setData({
      formId: e.detail.formId,
    })

    console.log(e)
    console.log(e.detail.value.name);
    
      my.request({
        url: that.data.ActivityJoin + that.data.id + "/join",
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          name: e.detail.value.name,
          phone: e.detail.value.phone,
          qqwx: e.detail.value.qqwx,
          count: e.detail.value.count
        },
        success: function (res) {
          console.log(that.data.price)
          console.log(res)
          var pay = res.data.data
          //发起支付
          var timeStamp = pay.timeStamp;
          var packages = pay.package;
          var paySign = pay.paySign;
          var nonceStr = pay.nonceStr;
          var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr };
          wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 500
          })
          that.pay(param)
        }

      })

    
  },
  /* 支付  */
  pay: function (param) {
    console.log("调起支付:" + param.timeStamp)
    var that = this
    wx.requestPayment({

      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,

      success: function (res) {
        // success
        console.log("支付成功");
        wx.showToast({
          title: '参加成功',
          icon: 'success',
          duration: 500
        })
        //发送模板消息
        that.sendMsg()
      },
      fail: function (res) {
        // fail
        console.log("支付失败")
        console.log(res)
        wx.showToast({
          title: '支付失败',
          icon: 'fail',
          duration: 2000
        })
      },
      complete: function () {
        // complete
        console.log("pay complete")
      }

    })
  },

  //发送模板消息
  sendMsg: function () {
    var that = this

    my.request({
      url: that.data.sendMsg,
      data: {
        formId: that.data.formId,
        id: that.data.id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)

        that.onLoad();
      }

    })
  },

  //获取凭证
  getToken: function () {
    var that = this
    my.request({
      url: "https://api.weixin.qq.com/cgi-bin/token",
      data: {
        grant_type: "client_credential",
        appid: "wx779bb08426a06b41",
        secret: "4836736a837bf29c974c213096a9e3b1"
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        
      }
    })
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function (options) {
    // 生命周期函数--监听页面显示
    // console.log("orderId+"+options.orderId)
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  },

})